<template>
  <div>
    <el-dialog :title="title" :visible.sync="dialogVisible" :before-close="handleCancel" center width="800px">
      <el-form ref="editFormRef" :model="editForm" :rules="rules" label-width="80px" size="small">
        <el-row>
          <el-col :span="12">
            <el-form-item label="类型ID" prop="positionSn">
              <el-input placeholder="请输入类型ID" v-model="editForm.positionSn"></el-input>
            </el-form-item>

            <el-form-item label="类型名" prop="positionName">
              <el-input placeholder="请输入类型名" v-model="editForm.positionName"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="概要" prop="positionDuty">
              <el-input
                placeholder="请输入概要"
                type="textarea"
                v-model="editForm.positionDuty"
                :maxlength="100"
                :autosize="{ minRows: 4, maxRows: 6 }"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div style="text-align: right">
        <el-button type="primary" @click="handleOk('editFormRef')" size="small">保 存</el-button>
        <el-button @click="handleCancel" size="small">关 闭</el-button>
      </div>

      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="date" label="日期" width="180"> </el-table-column>
        <el-table-column prop="name" label="姓名" width="180"> </el-table-column>
        <el-table-column prop="address" label="地址"> </el-table-column>
      </el-table>

      <!-- <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel" size="small">取 消</el-button>
        <el-button type="primary" @click="handleOk('editFormRef')" size="small">确 定</el-button>
      </div> -->
    </el-dialog>
  </div>
</template>

<script>
// import pick from 'lodash.pick'
// import { addPosition, modifyPosition } from '@/api/auth'

export default {
  name: 'StationModal',
  data () {
    return {
      title: '设备类型管理',
      dialogVisible: false,
      model: {},
      editForm: {
        positionSn: '',
        positionName: '',
        isEnable: '',
        positionDuty: '',
        remark: ''
      },
      rules: {
        positionSn: [{ required: true, message: '请输入岗位编号!' }],
        positionName: [{ required: true, message: '请输入岗位名称!' }],
        isEnable: [{ required: true, message: '请选择岗位状态!' }],
        positionDuty: [{ required: true, message: '请输入岗位职责!' }]
      },
      tableData: [
        {
          date: '2016-05-02',
          name: '王小虎',
          address: '上海市普陀区金沙江路1518 弄'
        },
        {
          date: '2016-05-04',
          name: '王小虎',
          address: '上海市普陀区金沙江路1517 弄'
        },
        {
          date: '2016-05-01',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1519 弄'
        },
        {
          date: '2016-05-03',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄'
        }
      ]
    }
  },
  methods: {
    // 新增
    add () {
      this.edit({})
    },

    // 编辑
    edit (record) {
      this.dialogVisible = true
      this.model = Object.assign({}, record)
      if (record.positionId) {
        this.title = '编辑岗位'
        // this.$nextTick(() => {
        //   this.editForm = pick(
        //     this.model,
        //     'positionSn',
        //     'positionName',
        //     'isEnable',
        //     'positionDuty',
        //     'remark'
        //   )
        // })
      } else {
        this.title = '添加岗位'
      }
    },

    // 提交表单
    handleOk (editFormRef) {
      this.$refs[editFormRef].validate(async valid => {
        if (valid) {
          let res, msg
          if (!this.model.positionId) {
            // res = await addPosition(this.editForm)
            msg = '保存成功！'
          } else {
            // res = await modifyPosition(
            //   Object.assign({}, this.editForm, {
            //     positionId: this.model.positionId
            //   })
            // )
            msg = '修改成功！'
          }
          if (res.code === 0) {
            this.$message.success(msg)
          } else {
            this.$message.warning(res.msg)
          }
          this.$emit('ok')
          this.handleCancel()
        }
      })
    },

    // 取消
    handleCancel () {
      this.dialogVisible = false
      this.$refs.editFormRef.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped></style>
